package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.query.LoginLogPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.LoginLogService;
import com.aloda.vo.LoginLogVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/loginlog")
public class LoginLogController {

    @Resource
    private LoginLogService loginLogService;

    /**
     * 分页查询
     * @param loginLogPageDTO
     * @return
     */
    @AdminAuthCheck
    @PostMapping("/page")
    public Result<PageResult<LoginLogVO>> pageQuery(@RequestBody LoginLogPageDTO loginLogPageDTO) {
        PageResult<LoginLogVO> pageResult = loginLogService.pageQuery(loginLogPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id获取
     * @param id
     * @return
     */
    @AdminAuthCheck
    @GetMapping("/{id}")
    public Result<LoginLogVO> getById(@PathVariable Long id) {
        return Result.success(BeanUtil.copyProperties(loginLogService.getById(id), LoginLogVO.class));
    }

    /**
     * 删除登录日志
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除登录日志")
    @AdminAuthCheck
    @DeleteMapping
    public Result delete(@RequestBody List<Long> ids) {
        return Result.success(loginLogService.removeBatchByIds(ids));
    }

    /**
     * 导出登录日志信息到Excel
     * @param response HttpServletResponse
     * @param ids 登录日志ID列表
     */
    @OperateLogAnno(category = "导出登录日志信息到Excel")
    @AdminAuthCheck
    @PostMapping("/export")
    public void exportLoginLogToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        loginLogService.exportLoginLogToExcel(response, ids);
    }
}
